home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr26 / netprog.zip / NETPROG.TAR / ipc / mainshmcli.c < prev    next >
C/C++ Source or Header  |  1989-12-17  |  2KB  |  85 lines

  1. #include    <stdio.h>
  2. #include    <sys/types.h>
  3. #include    <sys/ipc.h>
  4. #include    <sys/shm.h>
  5.  
  6. #include    "shm.h"
  7.  
  8. int    shmid, clisem, servsem;    /* shared memory and semaphore IDs */
  9. Mesg    *mesgptr;        /* ptr to message structure, which is
  10.                    in the shared memory segment */
  11.  
  12. main()
  13. {
  14.     /*
  15.      * Get the shared memory segment and attach it.
  16.      * The server must have already created it.
  17.      */
  18.  
  19.     if ( (shmid = shmget(SHMKEY, sizeof(Mesg), 0)) < 0)
  20.         err_sys("client: can't get shared memory segment");
  21.     if ( (mesgptr = (Mesg *) shmat(shmid, (char *) 0, 0)) == (Mesg *) -1)
  22.         err_sys("client: can't attach shared memory segment");
  23.  
  24.     /*
  25.      * Open the two semaphores.  The server must have
  26.      * created them already.
  27.      */
  28.  
  29.     if ( (clisem = sem_open(SEMKEY1)) < 0)
  30.         err_sys("client: can't open client semaphore");
  31.     if ( (servsem = sem_open(SEMKEY2)) < 0)
  32.         err_sys("client: can't open server semaphore");
  33.  
  34.     client();
  35.  
  36.     /*
  37.      * Detach and remove the shared memory segment and
  38.      * close the semaphores.
  39.      */
  40.  
  41.     if (shmdt(mesgptr) < 0)
  42.         err_sys("client: can't detach shared memory");
  43.     if (shmctl(shmid, IPC_RMID, (struct shmid_ds *) 0) < 0)
  44.         err_sys("client: can't remove shared memory");
  45.  
  46.     sem_close(clisem);    /* will remove the semaphore */
  47.     sem_close(servsem);    /* will remove the semaphore */
  48.  
  49.     exit(0);
  50. }
  51.  
  52. client()
  53. {
  54.     int    n;
  55.  
  56.     /*
  57.      * Read the filename from standard input, write it to shared memory.
  58.      */
  59.  
  60.     sem_wait(clisem);        /* get control of shared memory */
  61.     if (fgets(mesgptr->mesg_data, MAXMESGDATA, stdin) == NULL)
  62.         err_sys("filename read error");
  63.  
  64.     n = strlen(mesgptr->mesg_data);
  65.     if (mesgptr->mesg_data[n-1] == '\n')
  66.         n--;            /* ignore newline from fgets() */
  67.     mesgptr->mesg_len = n;
  68.     sem_signal(servsem);        /* wake up server */
  69.  
  70.     /*
  71.      * Wait for the server to place something in shared memory.
  72.      */
  73.  
  74.     sem_wait(clisem);        /* wait for server to process */
  75.     while( (n = mesgptr->mesg_len) > 0) {
  76.         if (write(1, mesgptr->mesg_data, n) != n)
  77.             err_sys("data write error");
  78.         sem_signal(servsem);    /* wake up server */
  79.         sem_wait(clisem);    /* wait for server to process */
  80.     }
  81.  
  82.     if (n < 0)
  83.         err_sys("data read error");
  84. }
  85.